<?php

namespace App\Criteria\Clbum\Clbum;

use App\Libs\Tools\DataTables;
use Illuminate\Support\Facades\Input;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;

class ClbumBaseQueryCriteria implements CriteriaInterface{

    public function apply($model, RepositoryInterface $repository){
        $model=$model->leftJoin('users','clbum.header_teacher_user_id','=','users.id')
                     ->leftJoin('grade','clbum.grade_id','=','grade.id')
                     ->select('clbum.*','users.name as header_teacher_name','grade.name as grade_name');
        $name=Input::get('name');
        $grade_name=Input::get('grade_name');
        if($name){$model=$model->where('clbum.name','like','%'.$name.'%');}
        if($grade_name){$model=$model->where('grade.name','like','%'.$grade_name.'%');}

        $order_column_tag=DataTables::get_order_column_tag();
        if(!is_null($order_column_tag)){
            $model->orderBy($order_column_tag['order_column'],$order_column_tag['order_type']);
        }
        return $model;
    }
}
